package com.solcoo.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solcoo.dao.DiJiaJuanDao;
import com.solcoo.pojo.DiJiaJuan;
import com.solcoo.service.DiJiaJuanService;

@Controller
public class DiJiaJuanController extends AbstractController {

	@Resource
    private DiJiaJuanService dijiajuanservice;
	@Resource
    private DiJiaJuanDao dijiajuandao;
	
	private static Logger logger = Logger.getLogger(DiJiaJuanController.class);
	
	// 添加一个新的 抵用卷
	@RequestMapping(value="shAddDjj.do")
	public @ResponseBody JsonResult addDjj(int shanghuid, int dijiazhi, String youxiaoriqi, String shoujih, String isApp) throws Exception {
		DiJiaJuan dijiajuan = new DiJiaJuan();
		dijiajuan.setShanghuid(shanghuid);
		dijiajuan.setDijiazhi(new Double(dijiazhi));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(youxiaoriqi);
		dijiajuan.setYouxiaoriqi(d);
		
		return dijiajuanservice.throwExAddDjj(dijiajuan, shoujih, isApp);
	}
	
	// 消费一张抵用卷
	@RequestMapping(value="shPayDjj.do")
	public @ResponseBody JsonResult payDjj(int id, String shoujih, String isApp) throws Exception {
		DiJiaJuan dijiajuan = dijiajuandao.queryAllDjjById(id);	
		logger.info("dijiajuan:" + dijiajuan.getId() + " jinge: " +  dijiajuan.getDijiazhi());
		return dijiajuanservice.throwExPayDjj(dijiajuan, shoujih, isApp);
	}
	
	// 取消一张抵用卷
	@RequestMapping(value="shCancelDjj.do")
	public @ResponseBody JsonResult cancelDjj(int id, String shoujih, String isApp) throws Exception {
		DiJiaJuan dijiajuan = dijiajuandao.queryAllDjjById(id);			
		return dijiajuanservice.throwExCancelDjj(dijiajuan, shoujih, isApp);
	}
	
	// 商户查看所有发放的 抵价卷, 既抵价卷历史记录
    @RequestMapping(value="shQueryDjj.do")
    public @ResponseBody Map<String, Object> listAllDjjBySh(String shanghuid) throws Exception {
    	logger.info("beging------------------");
    	return dijiajuanservice.getAllDjjBySH(Integer.parseInt(shanghuid.trim()));
    }
    
    
    // 用户查看所有 抵价卷, 既用户在该商家的抵价卷历史记录
    @RequestMapping(value="grQueryDjj.do")
    public @ResponseBody Map<String, Object> listAllDjjSh2Gr(String shanghuid, String gerenid) throws Exception {
    	return dijiajuanservice.getAllDjjSh2Gr(Integer.parseInt(shanghuid), Integer.parseInt(gerenid));
    }
    
    // 查看个人在某商家的可用抵价卷
    @RequestMapping(value="grQueryKeyongDjj.do")
    public @ResponseBody Map<String, Object> listAllKeyongDjj(String shanghuid, String gerenid) throws Exception {
    	return dijiajuanservice.getKeyongDjj(Integer.parseInt(shanghuid), Integer.parseInt(gerenid));
    }
}
